Preserve OCaml namespaces in generated module fields - #1847
Open
TheCBaH wants to merge 7 commits into
Open
Conversation
Translmod now hands out Runtime_fields.t for a unit's exports, so Lam_stats, Lam_coercion and J.program carry the namespace along with the identifier, and the CommonJS/ESM printers and the .cmj export table ask Runtime_fields for the name a field goes by at runtime rather than assuming it is the OCaml one. The duplicate-export guard compares those runtime names, which is what actually has to be unique in a JavaScript object. Mechanical: Runtime_fields.name is still the OCaml name, so the generated code is unchanged. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Extension constructors and classes now reach the printers with a runtime name of their own (Foo$extension, foo$class), which is what lets a structure export both an exception Foo and a module Foo instead of failing with "Foo are exported as twice". Mangled fields are additionally exposed under their plain OCaml name so that JavaScript callers keep working: in the unit's exports, and in nested module objects when the value is a variable -- a coerced field can be an arbitrary expression and an object literal has nowhere to bind it. Skipped when another field already answers to that name. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…e.js The node invocations resolved the runtime through the repository's checked-in node_modules, which the nix sandbox does not have; declaring the library makes dune materialize it under _build. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Member
|
I think I understand what this does, but it's unclear to me why we want it? Is there a failing test case that shows e.g. a name clash or similar? I'm curious what's the problem you're trying to solve with this. |
Contributor
Author
OCaml code in the wild, for example: https://github.com/dbuenzli/jsont/blob/main/src/jsont.ml#L24 |
anmonteiro
added a commit
that referenced
this pull request
Aug 4, 2026
Member
|
oh thank you! I wasn't even aware of that -- I reproduced this in #1851, and I'll take a look at this and check whether this is the fix we'll want to go with. Nevertheless, appreciate the report, but it's unclear to me yet that we'll want to go with this exact approach (it seems to make some of the names way worse, unfortunately). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
OCaml allows structure components from different namespaces to share a name. For example, an exception constructor and a module can both be named
Foo. JavaScript objects have a single property namespace, however, and Melange previously discarded the OCaml namespace while lowering module fields. As a result, the valid OCaml in the reproduction below failed to compile withFoo are exported as twice.This PR preserves each runtime field's OCaml namespace throughout compiler-libs and the Melange backend. The namespace is then used to give every field an unambiguous JavaScript name:
foofooFooFooFooFoo$extensionfoofoo$classValues and modules keep their existing names. Extension constructors and classes are also exported under their original names when those names are unambiguous, preserving compatibility for existing JavaScript consumers.
Reproduction
Previously, compiling this file failed:
With this change, both fields are emitted:
OCaml references to the exception use
Foo$extension, while references to the module continue to useFoo.Implementation
Shape.Sig_component_kind.t.Tests
The regression test covers:
include, andopen;